package claybird.dom;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import org.hibernate.mapping.Map;

import claybird.dom.Handicap.Categorie;

/*
 * Represent one user of the club
 */
@Entity
public class Person extends BaseEntity {
    private String name;
    private String forName;
    private boolean manager =false;
    private boolean guest =false;
    @OneToMany(mappedBy="person")
    private List<Handicap> listHandicap = new ArrayList<Handicap>();
    @OneToMany(mappedBy="person")
    private List<Result> listResult = new ArrayList<Result>();
    
    public Person(){
    	
    }
    
    public Person(String name, String forName,Boolean manager, Boolean invite){
    	this.name = name;
    	this.forName = forName;
    	this.manager = manager;
    	this.setGuest(invite);
    }
    
    
	public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getForName() {
        return forName;
    }
    public void setForName(String forName) {
        this.forName = forName;
    }
    
    public boolean isManager() {
        return manager;
    }
    public void setManager(boolean manager) {
        if (manager == true) 
            this.setGuest(false);
        this.manager = manager;
    }
    public boolean isGuest() {
        return guest;
    }
    public void setGuest(boolean guest) {
        this.guest = guest;
       
    }
    
    
    public Handicap getLastHandicap(){
    	Handicap lastHandicap = new Handicap();
    	if (this.listHandicap!=null){
	    	for(Handicap handic : this.listHandicap){
	    		if (lastHandicap==null || lastHandicap.getYear()<handic.getYear() ){
	    			lastHandicap =handic;
	    		}
	    		
	    	}
    	}
    	return lastHandicap;
    }

	public List<Handicap> getListHandicap() {
		return listHandicap;
	}

	public void setListHandicap(List<Handicap> listHandicap) {
		this.listHandicap = listHandicap;
	}

	public List<Result> getListResult() {
		return listResult;
	}

	public void setListResult(List<Result> listResult) {
		this.listResult = listResult;
	}
	public Result getResultFor(ShootDay day){
		for(Result res : this.listResult){
			if(res.getShootDay() == day){
				return res;
			}
		}
		return new Result(day,this);
	}

	public Handicap getLastHandicap(int year) {
    	Handicap lastHandicap = new Handicap();
		if (this.listHandicap!=null){
	    	for(Handicap handic : this.listHandicap){
	    		if (handic.getYear()==year ){
	    			lastHandicap =handic;
	    		}
	    		
	    	}
    	}
		return lastHandicap;
	}
	public int getTotalRanking(int year) {
		int total =0;
		List<Result> listRes = new ArrayList<Result>();
    	for (Result res : this.listResult){
    		Calendar myCalendar = GregorianCalendar.getInstance();
    		myCalendar.setTime(res.getShootDay().getDate());
    		
    		if (myCalendar.get(Calendar.YEAR)==year){
    			boolean isBetter = false;
    			for(Result test: listRes){
    				if (res.getRanking()<test.getRanking()){
    					listRes.add(listRes.indexOf(test), res);
    					isBetter=true;
    					break;
    				}
    			}
    			if (!isBetter){
    				listRes.add(res);
    			}
    		}
    	}
    	int leng= 4;
    	if(listRes.size()<4){
    		return 0;
    	}
    	for(int i=0;i<leng;i++){
    		
    		total = total+listRes.get(i).getRanking();
    	}
    	return total;
	}

}
